package com.ljf.tijian.handler;

import com.ljf.tijian.dto.ApiResponse;
import com.ljf.tijian.exception.TijianAppException;
import com.ljf.tijian.exception.TijianExceptionEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(TijianAppException.class)
    @ResponseBody
    public ApiResponse handleTijianAppException(TijianAppException e){
        e.printStackTrace();
        System.out.println("异常信息: "+e.getStackTrace());

        return new ApiResponse(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResponse handleAllException(Exception e){
        e.printStackTrace();
        System.out.println("异常信息:"+e.getStackTrace()+"message:"+e.getMessage());
        return new ApiResponse(TijianExceptionEnum.SYSTEM_ERROR.getCode(),TijianExceptionEnum.SYSTEM_ERROR.getMessage());
    }

}
